﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleObj : MonoBehaviour
{
    public string id;
    public string resUrl;
    public float duration;
    public bool Isloop;

    GameObject particleObj = null;
    float curDeltaTime = 0f;
    bool isPlaying = false;
    bool isAutoRecovery = true;

    public void Init(ParticleData pd)
    {
        id = pd.particleId;
        resUrl = pd.resUrl;
        duration = pd.duration;
        Isloop = pd.IsLoop;
        particleObj = null;
        curDeltaTime = 0f;
        isPlaying = false;
        isAutoRecovery = true;

        Load();
    }

    void Load()
    {
        particleObj = ParticleFactory.SpawnParticle(resUrl);
        particleObj.transform.SetParent(transform);
        particleObj.transform.localPosition = Vector3.zero;
        particleObj.transform.localScale = Vector3.one;
        particleObj.transform.localEulerAngles = Vector3.zero;
        particleObj.SetActive(false);
    }

    void Unload()
    {
        if (particleObj != null)
        {
            ParticleFactory.UnSpawnParticle(particleObj);
        }
        particleObj = null;
    }

    public void Play(int layer, Transform parent)
    {
        Play(layer, parent, Vector3.zero, 1f, Vector3.zero, true);
    }

    public void Play(int layer, Transform parent, Vector3 pos, float scale, Vector3 euler, bool isAutoRecovery)
    {
        gameObject.SetActive(true);
        transform.SetParent(parent);
        transform.localPosition = pos;
        transform.localScale = Vector3.one;
        transform.localEulerAngles = euler;

        if (scale != 1f)
        {
            if (particleObj.GetComponent<ParticleScaler>() != null)
            {
                particleObj.GetComponent<ParticleScaler>().particleScale = scale;
            }
            else
            {
                var scaler = particleObj.AddComponent<ParticleScaler>();
                scaler.particleScale = scale;
            }
        }
        else
        {
            if (particleObj.GetComponent<ParticleScaler>() != null)
            {
                particleObj.GetComponent<ParticleScaler>().particleScale = 1f;
            }
        }

        CommonMethod.ChangeLayer(transform, layer);

        particleObj.SetActive(false);
        particleObj.SetActive(true);

        this.isAutoRecovery = isAutoRecovery;
        isPlaying = true;
        curDeltaTime = 0f;
    }

    void Update()
    {
        if (isPlaying)
        {
            curDeltaTime += Time.deltaTime;

            if (isAutoRecovery)
            {
                if (curDeltaTime >= duration)
                {
                    Recycle();
                }
            }
        }
    }

    public void Recycle()
    {
        Unload();
        GameObject.Destroy(gameObject);
    }

    public void Show()
    {
        if (particleObj != null)
        {
            particleObj.SetActive(true);
        }
    }

    public void Hide()
    {
        if (particleObj != null)
        {
            particleObj.SetActive(false);
        }
    }

    public bool IsPlaying()
    {
        return isPlaying;
    }
}
